package com.mohe.zhihui.action;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.mohe.zhihui.model.Activity;
import com.mohe.zhihui.model.Apply;
import com.mohe.zhihui.model.Message;
import com.mohe.zhihui.model.Project;
import com.mohe.zhihui.service.interfaces.IActivity;
import com.mohe.zhihui.service.interfaces.IApply;
import com.mohe.zhihui.service.interfaces.IMessage;
import com.mohe.zhihui.service.interfaces.IProject;
import com.mohe.zhihui.utils.Json;
import com.mohe.zhihui.utils.JsonUtil;
import com.mohe.zhihui.utils.Page;
import com.opensymphony.xwork2.ActionSupport;

public class ActivityInfoAction extends ActionSupport {

	private static final long serialVersionUID = -3009140220217402655L;
//	ApplicationContext ac;
	
	private IActivity activityServ;
	
	private List<Activity> activityList;
	private Activity activity = new Activity();
	private Page page = new Page();
	
	private String liIndex;
	
	public IActivity getActivityServ() {
		return activityServ;
	}

	public void setActivityServ(IActivity activityServ) {
		this.activityServ = activityServ;
	}

	public List<Activity> getActivityList() {
		return activityList;
	}

	public void setActivityList(List<Activity> activityList) {
		this.activityList = activityList;
	}
	
	public Activity getActivity() {
		return activity;
	}

	public void setActivity(Activity activity) {
		this.activity = activity;
	}

	public Page getPage() {
		return page;
	}

	public void setPage(Page page) {
		this.page = page;
	}

	public String getLiIndex() {
		return liIndex;
	}

	public void setLiIndex(String liIndex) {
		this.liIndex = liIndex;
	}

	public String getActivityListForPage() {
		int count = activityServ.getCount(activity);
		page.setPageSize(8);
		page.setCount(count);
		activityList = activityServ.getActivityList(activity, page);
		return "activity.jsp";
	}
	
	public void searchActivityList() {
		Json json = new Json();
		List<Map<String, String>> mapList = new ArrayList<Map<String, String>>();
		int count = activityServ.getCount(activity);
		page.setPageSize(8);
		page.setCount(count);
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		List<Activity> list = activityServ.getActivityList(activity, page);
		if (list != null) {
			int index = (page.getCurrPage() - 1) * page.getPageSize() + 1;
			for (Activity s : list) {
				Map<String, String> map = new HashMap<String, String>();
				map.put("index", index + "");
				if (s.getTimeKey() != null) {
					map.put("timeKey", s.getTimeKey() + "");
				} else {
					map.put("timeKey", "0");
				}
				if (!StringUtils.isBlank(s.getTitle())) {
					map.put("title", s.getTitle());
				} else {
					map.put("title", "");
				}
				if (!StringUtils.isBlank(s.getContent())) {
					map.put("content", s.getContent());
				} else {
					map.put("content", "");
				}
				if (s.getCreateDate() != null) {
					map.put("date", df.format(s.getCreateDate()));
				} else {
					map.put("date", "");
				}
				mapList.add(map);
				index++;
			}
		}
		json.setObject(page);
		json.setMap(mapList);
		JsonUtil.writeJson(json);
	}

	public String toActivityIndex() {
		int count = activityServ.getCount(activity);
		page.setCount(count);
		List<Activity> tmp = activityServ.getActivityList(activity, page);
		activityList = new ArrayList<Activity>();
		if(tmp != null && tmp.size() > 4){
			for(int i = 0; i < 5; i++){ // 取前五个活动的图片
				activityList.add(tmp.get(i));
			}
		}
		return "activityIndex.jsp";
	}
	
}
